package com.geekchique.runningbuddy.activity;


import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.Filterable;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.geekchique.runningbuddy.R;
import com.geekchique.runningbuddy.activity.stopclock.StopClockTools;
import com.geekchique.runningbuddy.model.JogEntry;
import com.geekchique.runningbuddy.util.DBHelper;
import com.geekchique.runningbuddy.view.TodayView;


public class Today extends ListActivity {

	private DBHelper dbHelper;
	private TodayView todayView;

	@Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.today);
        dbHelper = new DBHelper(this);
		
        createTodayView();
        
		populateTodaysJoggingTotals();
        
        populateTodaysJoggingEntries();
    }

	private void createTodayView() {
		this.todayView = new TodayView(
										this,
										(TextView)findViewById(R.id.miles),
										(TextView)findViewById(R.id.heading_jogname),
										(TextView)findViewById(R.id.timerun)
										);
	}

	private void populateTodaysJoggingTotals() {
		this.todayView.setMiles("00");
	}

	private void populateTodaysJoggingEntries() {
		List<JogEntry> jogEntries = getTodaysJogEntries();
		writeOutAJogDetails(jogEntries.get(jogEntries.size() - 1));
		writeOutDetailsOfAllJogsToday(jogEntries);
	}

	private void writeOutDetailsOfAllJogsToday(List<JogEntry> jogEntries) {
		int i = 0;
		for(JogEntry entry: jogEntries){
			if(i < 2){
				writeOutAJogDetails(entry);
			}else{
				break;
			}
			i++;
		}
	}


	private void writeOutAJogDetails(JogEntry entry) {
		long longOfTimeRun = entry.stopTime - entry.startTime;
		
        // Get a cursor with all jogs

        Cursor c = dbHelper.getCursorOfAllJogs();
        startManagingCursor(c);

//        ListAdapter adapter = new SimpleCursorAdapter(this,
//                R.layout.jog_entry, 
//                c,
//                new String[] {"name", "stopTime"}, 
//                new int[] {R.id.heading_jogname, R.id.timerun});
        
        ListAdapter adapter = new JogEntryListAdaptor(this,
        		R.layout.jog_entry, 
        		c,
        		new String[] {"name", "stopTime"}, 
        		new int[] {R.id.heading_jogname, R.id.timerun}); 
        
        setListAdapter(adapter);
		
	
//		RelativeLayout.LayoutParams params = new android.widget.RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//		
//		this.addContentView(jogEntry, params);

//		this.todayView.setHeading_jogname(entry.name);
//		this.todayView.setTimerun(TimerStringTools.getTimeInExpectedFormat(longOfTimeRun));
	}

	private ArrayList<JogEntry> getTodaysJogEntries() {
		List<JogEntry> jogEntries = dbHelper.fetchAllJogEntries();
		List<JogEntry> entriesToday = new ArrayList<JogEntry>();
		
		for(JogEntry jogEntry:jogEntries){
			if(StopClockTools.inTheSameDay(System.currentTimeMillis(), jogEntry.stopTime)) {
				entriesToday.add(jogEntry);
			} 
		}
		
		return (ArrayList<JogEntry>) entriesToday;
	}
	
	public class JogEntryListAdaptor extends CursorAdapter implements Filterable{

		private ContentResolver content;

		public JogEntryListAdaptor(Cursor c, Context context) {
			super(c, context);
            content = context.getContentResolver();
		}

		public JogEntryListAdaptor(Context context, int jog_entry, Cursor c, String[] strings, int[] is) {
			super(c, context);
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
            ((TextView) view).setText(cursor, 5);
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
            TextView view = new TextView(context);
            view.setText(cursor, 5);
            return view;
		}
		
	}
	
	

}
